package com.gaoding.shared.dict.sdk.context;

import com.gaoding.commons.exception.NotFoundException;
import com.gaoding.commons.util.SpringUtil;
import com.gaoding.shared.dict.sdk.configuration.DictSdkProperties;
import com.gaoding.shared.dict.sdk.configuration.SpringApplicationProperties;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 字典sdk上下文
 * @Author: 西格 xige@gaoding.com
 * @date 2021-02-04 11:23
 */
public class DictSdkContext {
    private final static long ONE_DAY = 24 * 60 * 60 * 1000L;
    private final static long FIVE_MIN = 5 * 60 * 1000L;

    public static DictSdkContext INSTANCE = new DictSdkContext();
    private DictSdkProperties dictSdkProperties = SpringUtil.getBean("dictSdkProperties");
    private SpringApplicationProperties springApplicationProperties = SpringUtil.getBean("springApplicationProperties");


    /**
     * 获取所有配置的服务编码
     *
     * @return 服务编码集合
     */
    public List<String> getAllServiceCode() {
        List<String> serviceCodes;
        String serviceCode = springApplicationProperties.getName();
        if (StringUtils.isEmpty(serviceCode)) {
            throw new NotFoundException("spring.application.name不存在");
        }

        String multiApplicationNames = dictSdkProperties.getMultiApplicationNames();
        if (StringUtils.isNotBlank(multiApplicationNames)) {
            serviceCodes = Arrays.stream(multiApplicationNames.split(",")).collect(Collectors.toList());
        } else {
            serviceCodes = Collections.singletonList(serviceCode);
        }
        return serviceCodes;
    }


    public long getFullUpdateInitialDelay() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getFullUpdateInitDelayTime(), 0L);
    }

    public long getFullUpdateDelay() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getFullUpdateDelay(), ONE_DAY);
    }

    public long getIncrementalUpdateInitialDelay() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getIncrementUpdateInitDelayTime(), FIVE_MIN);
    }

    public long getIncrementalUpdateDelay() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getIncrementUpdateDelay(), FIVE_MIN);
    }

    public long getEachServiceLoadDataTimeoutTime() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getTimeoutEachServiceDataLoad(), 20000L);
    }

    public long getLoadDataTimeoutTime() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getTimeoutServiceDataLoad(), 20000L);
    }

    public long getInitWaitTimeoutTime() {
        return parseValueIfNotExistSetDefault(dictSdkProperties.getTimeoutInitData(), 30000L);
    }

    public Boolean getIsThrow() {
        return dictSdkProperties.getThrowEx();
    }


    private long parseValueIfNotExistSetDefault(String value, long defaultValue) {
        if (StringUtils.isEmpty(value)) {
            return defaultValue;
        }

        long longValue = Long.parseLong(value);
        return longValue > 0 ? longValue : defaultValue;
    }

}
